<?php

if (!function_exists('getCoutProfesseurSaison')) {
	function getCoutProfesseurSaison($db, $proidpro, $saiidsai) {
		$sql = "SELECT proidpro, promotxh, sainbsem, actlbhdb, actlbdfn ".
			"FROM gyapro, gyaact, gyasai ".
			"WHERE proidpro = actidpro ".
			"and actidsai = saiidsai ".
			"and proidpro = ? and saiidsai = ?";
		$query = $db->query($sql, array((int)$proidpro, (int)$saiidsai));

		// recuperer les enregistrements
		if ($query->num_rows() == 0) {
			return null;
		}
		$coutTotal = 0;
		foreach ($query->result_array() as $row) {
			$promotxh = $row['promotxh'];
			$sainbsem = $row['sainbsem'];
			$actlbhdb = toHoursDecimal( $row['actlbhdb'] );
			$actlbdfn = toHoursDecimal( $row['actlbdfn'] );
			$duree = $actlbdfn - $actlbhdb;
			$coutActivite = $promotxh * $duree * $sainbsem;
			$coutTotal += $coutActivite;
		}
		return $coutTotal;
	}
}



if (!function_exists('getChiffreAffairesActivite')) {
	function getChiffreAffairesActivite($db, $actidact) {
		$sql = "SELECT actidact, actmocot, count(adhidadh) nbadh ".
			"FROM gyaact, gyaxaa, gyaadh ".
			"WHERE actidact = xaaidact ".
			"and xaaidadh = adhidadh ".
			"and actidact = ? ".
			"group by actidact, actmocot";
		$query = $db->query($sql, array((int)$actidact));

		// recuperer les enregistrements
		if ($query->num_rows() == 0) {
			return null;
		}
		$row = $query->row_array();
		$actmocot = $row['actmocot'];
		$nbadh = $row['nbadh'];
		return $actmocot * $nbadh;
	}
}


if (!function_exists('getNbAdherentsActivite')) {
	function getNbAdherentsActivite($db, $actidact) {
		$sql = "SELECT actidact, count(adhidadh) nbadh ".
			"FROM gyaact, gyaxaa, gyaadh ".
			"WHERE actidact = xaaidact ".
			"and xaaidadh = adhidadh ".
			"and actidact = ? ";
		$query = $db->query($sql, array((int)$actidact));

		// recuperer les enregistrements
		if ($query->num_rows() == 0) {
			return null;
		}
		$row = $query->row_array();
		$nbadh = $row['nbadh'];
		return $nbadh;
	}
}



if (!function_exists('toHoursDecimal')) {
	function toHoursDecimal($hours_h_min) {
		$hoursMins = explode('h', $hours_h_min);
		return $hoursMins[0] + ($hoursMins[1]/60);
	}
}


?>